/*
 The MIT License
 
 Copyright InScopeApps{+} 2009.
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */


#import "inscp_paths.h"

#define kUnixSlash      @"/"
#define kWindowsSlash   @"\\"
#define kBlank          @""

@implementation inscp_paths

@synthesize paths;

- (id)init{
    self = [super init];
	if(self){
        self.paths = [[NSMutableArray alloc] init];
    }
    return self;
}

#pragma mark -
#pragma mark NSMutableArray wrappers

- (NSUInteger)count{
    return [self.paths count];
}

#pragma mark -
#pragma mark path functions
- (void)addPath:(NSString *)path{
    if(path){
        NSURL *urlPath = [[NSURL alloc] initWithString:path];
        [paths addObject:[[urlPath path] stringByStandardizingPath]];
        [urlPath release];
    }
}

//truncate paths to last component
- (void)lastComponent{
    NSUInteger i, arrayCount = [self count];
    NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
    for(i = 0; i<arrayCount; i++){
        NSString *path = [[paths objectAtIndex:i] lastPathComponent];
        [paths replaceObjectAtIndex:i withObject:path];
    }
    [pool release];
}

//truncate paths
- (void)truncatePaths:(NSUInteger)value{
    if(value > 0){
        NSUInteger i, ii, arrayCount = [self count];
        NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
        for(i = 0; i<arrayCount; i++){
            if(value == 1){
                [paths replaceObjectAtIndex:i withObject:[[paths objectAtIndex:i] lastPathComponent]];
            }
            else{
                NSArray *pathParts = [[paths objectAtIndex:i] pathComponents];
                
                NSUInteger pathPartsCount = [pathParts count];
                NSInteger truncateValue = pathPartsCount-value;
                
                //ignore the leading slash if we are working on the first component
                if([[pathParts objectAtIndex:0] isEqualTo:kUnixSlash] && truncateValue==1){
                    truncateValue--;
                }
                
                NSString *path = kBlank;
                if(truncateValue > 0){
                    for(ii=truncateValue; ii<pathPartsCount; ii++){
                        path = [path stringByAppendingPathComponent:[pathParts objectAtIndex:ii]];
                    }
                    NSLog(@"%d - %@", truncateValue, path); 
                    [paths replaceObjectAtIndex:i withObject:path];
                }
            }
        }
        [pool release];
    }
}

//search and replace in each path
- (void)search:(NSString *)search replace:(NSString *)replace{
    if(search == nil){
        search = kBlank;
    }
    if(replace == nil){
        replace = kBlank;
    }
    
    if([search isNotEqualTo:kBlank]){
        NSUInteger i, arrayCount = [self count];
        NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
        for(i = 0; i<arrayCount; i++){
            NSString *path = [[paths objectAtIndex:i] stringByReplacingOccurrencesOfString:search withString:replace];
            [paths replaceObjectAtIndex:i withObject:path];
        }
        [pool release];
    }
}

//add a string to the front and back of each path
- (void)pre:(NSString *)pre post:(NSString *)post{
    if(pre == nil){
        pre = kBlank;
    }
    if(post == nil){
        post = kBlank;
    }
    
    if(!([pre isEqualTo:kBlank] && [post isEqualTo:kBlank])){
        NSUInteger i, arrayCount = [self count];
        NSAutoreleasePool *pool =  [[NSAutoreleasePool alloc] init];
        for(i = 0; i<arrayCount; i++){
            NSString *path = [[NSString alloc] initWithFormat:@"%@%@%@", pre, [paths objectAtIndex:i], post];
            [paths replaceObjectAtIndex:i withObject:path];
        }
        [pool release];
    }
}

#pragma mark -
#pragma mark block formating
- (NSString *)pathsJoinedByString:(NSString *)string{
    return [self.paths componentsJoinedByString:string];
}

#pragma mark -
#pragma mark dealloc
- (void)dealloc{
	[paths release];
    
	[super dealloc];
}

@end
